package vip.zcn.webmanagement.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.zcn.webmanagement.mapper.EmpMapper;
import vip.zcn.webmanagement.pojo.Emp;
import vip.zcn.webmanagement.pojo.PageBean;
import vip.zcn.webmanagement.service.EmpService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    // @Override
    // public PageBean page(Integer page, Integer pageSize) {
    //
    //     // 1. 获取总记录数
    //     Long count = empMapper.count();
    //     Integer start = (page - 1) * pageSize;
    //     // 2. 获取分页列表
    //     List<Emp> empList = empMapper.list(start, pageSize);
    //     // 3. 封装PageBean对象
    //     PageBean<Emp> pageBean = new PageBean<>();
    //     pageBean.setRows(empList);
    //     pageBean.setTotal(count);
    //
    //     return pageBean;
    // }


    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {

        // 设置分页插件
        PageHelper.startPage(page, pageSize);
        //  查询列表
        List<Emp> list = empMapper.list(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) list;
        //  封装PageBean
        PageBean pageBean = new PageBean();
        pageBean.setTotal(p.getTotal());
        pageBean.setRows(p.getResult());



        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = empMapper.selectById(id);
        return emp;
    }

    @Override
    public void update(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        Emp e = empMapper.getEmpByUsernameAndPassword(emp);
        return e;
    }


}
